package com.feng.oauth2.oauth.infrastructure.jdbc;


import com.feng.oauth2.oauth.domain.userUrl.UserUrls;
import com.feng.oauth2.oauth.domain.userUrl.UserUrlsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Repository("userUrlsRepositoryJdbc")
public class UserUrlsRepositoryJdbc implements UserUrlsRepository {


    private static UserUrlRowMapper userUrlRowMapper = new UserUrlRowMapper();

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public UserUrls findUserUrlsById(Integer id) {
        final String sql = " select * from user_urls where  id = ? ";
        final List<UserUrls> list = this.jdbcTemplate.query(sql, new Object[]{id}, userUrlRowMapper);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<UserUrls> findAllUserUrls(String username) {
        final String sql = " select * from user_urls where  username = ? ";
        return this.jdbcTemplate.query(sql, new Object[]{username}, userUrlRowMapper);
    }

    @Override
    public void saveUserUrls(UserUrls userUrls) {
        final String sql = " insert into user_urls(username,web_url) values (?,?) ";
        this.jdbcTemplate.update(sql, new Object[]{userUrls.getUsername(),userUrls.getWebUrl()});
    }

    @Override
    public void updateUserUrls(UserUrls userUrls) {
        final String sql = " update  user_urls set username=?, web_url=? where id=?";
        this.jdbcTemplate.update(sql, new Object[]{userUrls.getUsername(),userUrls.getWebUrl(),userUrls.getId()}, userUrlRowMapper);
    }

    @Override
    public void deleteUserUrls(Integer id) {
        final String sql = " delete from user_urls  where id=?";
        this.jdbcTemplate.update(sql, new Object[]{id}, userUrlRowMapper);
    }

    @Override
    public void saveAllUrls(List<UserUrls> urlList) {
        final List<UserUrls> userUrlsList = urlList;
        final String sql = " insert into user_urls(username,web_url) values (?,?) ";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                String username=userUrlsList.get(i).getUsername();
                String web_url=userUrlsList.get(i).getWebUrl();
                ps.setString(1, username);
                ps.setString(2, web_url);
            }
            
            @Override
            public int getBatchSize() {
                return userUrlsList.size();
            }
        });
    }

    @Override
    public void deleteUserAllUrls(String username) {
        final String sql = " delete from user_urls  where username=?";
        this.jdbcTemplate.update(sql, new Object[]{username});
        
    }
}
